<?php


namespace app\agent\controller\agent;

use app\agent\controller\Controller;
use app\agent\model\agent\Agent as AgentModel;
use app\agent\model\agent\User as AgentUserModel;
use think\App;
use think\Db;
use think\Session;
use utils\Random;
use think\Config;


class Partner  extends Controller{
    /**
     * 合伙人首页
     */
    public function index(){
        $session = Session::get('yoshop_agent');
        $agent_id = $session['agent']['agent_id'];
        $model = new AgentModel;
        $return=$model->getPartnerList($agent_id,2);
        /*$return=$return->toArray();
        if(!empty($return['data'])){
            foreach ($return['data'] as $k=>$v){
                $in=Db::name('agent')->where(['agent_id'=>$v['recommend_id']])->find();
                if($in['type']==0){
                    $return['data'][$k]['recommend_typename']='品牌商';
                }elseif ($in['type']==1){
                    $return['data'][$k]['recommend_typename']='城市运营商';
                }elseif ($in['type']==2){
                    $return['data'][$k]['recommend_typename']='城市合伙人';
                }else{
                    $return['data'][$k]['recommend_typename']='代言人';
                }
                $return['data'][$k]['recommend_realname']=$v['real_name'];
            }
        }*/
        return $this->fetch('index', [
            'list' => $list =$return,
        ]);
    }

    /**
     * 添加合伙人
     */
    public function add(){
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        $model = new AgentModel;
        $session = Session::get('yoshop_agent');
        $data = $this->postData('agent');

        $infos1=Db::name('agent')->where(['user_name'=>$data['user_name']])->find();
        if(!empty($infos1)){
            return $this->renderError('登录名不能重复');
        }
        Db::startTrans();
        try{
            $data['recommend_id'] = $session['agent']['agent_id'];

            $agentOneInfo=Db::name('agent')->where(['pinpai_id'=>$session['agent']['pinpai_id'],'user_name'=>$data['user_name']])->find();
            if($agentOneInfo){
                return $this->renderError($model->getError() ?: '登录名已被占用，请重新输入');
            }
            if($data['password']!=$data['password_confirm']){
                return $this->renderError($model->getError() ?: '两次输入的密码不一致，添加失败');
            }
            //手机号码格式验证
            if(!preg_match("/^[1]([3-9])[0-9]{9}$/",$data['company_tel'])){
                return $this->renderError('联系电话格式有误请重新输入');
            }
            if($data['level']==0){
                return $this->renderError('请选择合伙人性质');
            }
            $dataInfo['prefix']='';//所属品牌商登录名前缀
            $dataInfo['pinpai_id']=$session['agent']['pinpai_id'];//所属品牌商id
            $dataInfo['recommend_id']=$data['recommend_id'];//上级id（0，系统添加，没有上级推荐人）
            $dataInfo['company_name']=$data['real_name'];//公司名称用合伙人名称
            $dataInfo['type']=$data['type'];//代理商身份（0，品牌商，1.城市运营商，2.城市合伙人，3.代言人）
            $dataInfo['real_name']=$data['real_name'];//负责人姓名
            $dataInfo['company_tel']=$data['company_tel'];//服务商联系电话
            $dataInfo['level']=$data['level'];//1:个人  2：企业
            $dataInfo['service_charge']=$data['service_charge'];//服务商参与的分红百分比
            //$dataInfo['partner_reward']=$data['reward'];//推荐合伙人奖励
            //$dataInfo['operate_reward']=$data['operate_reward'];//服务商推荐获得的奖金
            $dataInfo['user_name']=$data['user_name'];//登录账户名称
            $salt=Random::alnum();
            $dataInfo['salt']=$salt;//密码盐
            $dataInfo['password']= md5(md5($data['password'].$salt.config('salt')));
            //$dataInfo['reward']=$data['reward'];//服务商推荐获得的奖金
            $dataInfo['is_recycle']=0;//是否回收
            $dataInfo['is_delete']=0;//是否删除
            $dataInfo['status']=1;//是否启用
            $dataInfo['is_super']=1;//是否超级管理员
            $dataInfo['create_time']=time();//创建时间
            $dataInfo['update_time']=0;//更新时间
            $dataInfo['finish_time']=time()+864000;//过期时间
            $dataInfo['active']=0;//账户激活（0，未激活，1.已激活）
            if($data['level']==1){
                $dataInfo['employees_num']=0;
            }else{
                $dataInfo['employees_num']=$data['employees_num'];
            }
            $dataInfo['agent_check_status']=1;
            $dataInfo['admin_check_status']=1;
            //新增记录
            $agentId=$model->add($dataInfo);
            //新增agent_user数据
            $agentUserModel=new AgentUserModel();
            $dataInfo['agent_id']=$agentId;
            $agentUserModel->add($dataInfo);
            Db::commit();
            return $this->renderSuccess('添加成功',url('agent.partner/index'));
        } catch (\Exception $e) {
            Db::rollback();
            return $this->renderError('添加失败');
        }
    }
}